/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.karol.lx.agent;

import java.util.logging.Level;
import java.util.logging.Logger;
import org.karol.lx.model.GradeRequest;

/**
 *
 * @author karol
 */
public class Grader implements Runnable {

    private int mStatus;
    private Thread mMainThread;
    public static final int STATUS_IDLE = 1;
    public static final int STATUS_INITIALIZING = 0;
    public static final int STATUS_WORKING = 2;
    private GradeRequest mCurrentRequest = null;
    private String mId;

    public Grader(String id) {
        mId = id;
        mStatus = STATUS_INITIALIZING;
        mMainThread = new Thread(this);
        mMainThread.start();
    }

    public synchronized void grade(GradeRequest pRequest) {
        mCurrentRequest = pRequest;
        notify();
    }

    public int getStatus() {
        return mStatus;
    }

    @Override
    public synchronized void run() {
        while (true) {
            Logger.getLogger(mId).log(Level.INFO , "Idle after {0}", mCurrentRequest);
            mStatus = STATUS_IDLE;
            try {
                wait();
            } catch (InterruptedException ex) {
                Logger.getLogger(Grader.class.getName()).log(Level.SEVERE, null, ex);
            }
            
            Logger.getLogger(mId).log(Level.INFO , "Working for {0}", mCurrentRequest.toString());
            mStatus = STATUS_WORKING;
            //TODO: Implement grading logic here
        }
    }
}
